using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Lizk.Utilities
{
    /// <summary>
    /// Encapsulates simple INI file operations
    /// </summary>
    public class INISettings
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// Writes a value to an ini file.
        /// </summary>
        /// <param name="path">The path of the ini file to write to.</param>
        /// <param name="Section">The section to write the value to eg. "test" writes to [test]</param>
        /// <param name="Key">The key to write eg: key=value</param>
        /// <param name="Value">The value to write eg: key=value</param>
        public static bool IniWriteValue(string path, string Section, string Key, string Value)
        {
            return 0 != WritePrivateProfileString(Section, Key, Value, path);
        }
        /// <summary>
        /// Reads a value from an ini file. 
        /// </summary>
        /// <param name="path">Path of the INI file to read.</param>
        /// <param name="Section">Section to read the value from.</param>
        /// <param name="Key">Key of the value</param>
        /// <returns>The value of the key or NULL if not found.</returns>
        public static string IniReadValue(string path, string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            if (i < 0)
                return null;
            return temp.ToString();

        }
    }
}
